<?php
header("HTTP/1.1 200 OK");
header("T: 2.0");
define( '_VALID_MOS' , 1 );
$pathroot = dirname(dirname(__FILE__));
require_once($pathroot.'/common.inc.php');

$_SERVER["REQUEST_URI"] = str_replace( "//","/",$_SERVER["REQUEST_URI"]);
$webFileArray = explode("/",$_SERVER["REQUEST_URI"]);

//$_SERVER["HTTP_X_REWRITE_URL"] = str_replace( "//","/",$_SERVER["HTTP_X_REWRITE_URL"]);
//$webFileArray = explode("/",$_SERVER["HTTP_X_REWRITE_URL"]);

switch ($webFileArray[1])
{
	case "category":
		if(empty($webFileArray[2])){
			include_once( $pathroot."/corporation_index.php" );
		}else{
			checkCatePath();
			include_once( $pathroot."/category.php" );
		}
		break;
	case "corporation":
		if(empty($webFileArray[2])){
			include_once( $pathroot."/corporation_index.php" );
		}else{
			if(checkCorPath()){
				include_once( $pathroot."/corporation.php" );
			}else{
				if(checkCorProPath()){
					include_once( $pathroot."/corporation_profile.php" );
				}elseif(checkInnerIndexPath()){
					include_once( $pathroot."/corporation.php" );
				}elseif(checkCommentPath()){
					include_once( $pathroot."/corporation_comment.php" );
				}else{
					show404();
				}
			}
		}
		break;
	case "tech":
		checkTech();
		include_once( $pathroot."/tech.php" );
		break;
	default:
		show404();
		break;
}

function checkCorPath()
{
	preg_match('#/corporation/(\d*)[_]?(\d*).html#i',$_SERVER["REQUEST_URI"],$matches);
	if($matches[1] > 0){
		$_REQUEST['id'] = $matches[1];
		if($matches[2] > 0){
			$_REQUEST['p'] = $matches[2];
		}
		return true;
	}else{
		return false;
	}
}

function checkCorProPath()
{
	preg_match('#/corporation/(\d*)/profile.html#i',$_SERVER["REQUEST_URI"],$matches);
	if($matches[1] > 0){
		$_REQUEST['id'] = $matches[1];
		return true;
	}else{
		return false;
	}
}

/*生成评论详细页面*/
function checkCommentPath()
{
	preg_match('#/corporation/(\d+)/comment_(\d+).html#i',$_SERVER["REQUEST_URI"],$matches);
	if($matches[1] > 0){
		$_REQUEST['id'] = $matches[1];
		$_REQUEST['p'] = $matches[2];
		return true;
	}else{
		return false;
	}
}

function checkInnerIndexPath(){
	preg_match('#/corporation/(\d*)/$#i',$_SERVER["REQUEST_URI"],$matches);
	if($matches[1] > 0){
		$_REQUEST['id'] = $matches[1];
		return true;
	}else{
		return false;
	}
}

function checkCatePath()
{
	preg_match('#/category/(\d*)_(\d*)_(\d*)_(\d*).html#i',$_SERVER["REQUEST_URI"],$matches);
	if($matches[1] > 0 ){
		$_REQUEST['cateid'] = $matches[1];
		$_REQUEST['scaleid'] = $matches[2];
		$_REQUEST['typeid'] = $matches[3];
		$_REQUEST['p'] = $matches[4];
	}else{
		show404();
	}
}

function checkTech()
{
	preg_match('#/tech/(\d*).html#i',$_SERVER["REQUEST_URI"],$matches);
	if($matches[1] > 0 ){
		$_REQUEST['id'] = $matches[1];
	}else{
		show404();
	}
}

function show404(){
	header("Location: /404.php");
	die;
}
?>